package com.lex.service.impl;

import com.lex.common.redis.RedisService;
import com.lex.data.admin.model.LexCmsMenu;
import com.lex.data.admin.model.LexCmsUsers;
import com.lex.service.SystemCacheService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 缓存实现类，用户缓存用户数据、资源列表和菜单列表
 *
 */
@Service
public class SystemCacheServiceImpl implements SystemCacheService {

    @Resource
    private RedisService redisService;

    @Value("${redis.database}")
    private String REDIS_DATABASE;

    @Value("${redis.key-prefix}")
    private String REDIS_PREFIX;

    @Value("${redis.expire.common}")
    private Long REDIS_EXPIRE;

    @Value("${redis.key-resource}")
    private String REDIS_KEY_RESOURCE;

    @Value("${jwt.refresh_expiration}")
    private Long REFRESH_EXPIRATION;


    @Override
    public void delUser(String username) {
        String key = REDIS_DATABASE + ":" + REDIS_PREFIX + ":" + username;
        redisService.del(key);
    }

    @Override
    public LexCmsUsers getLexCmsUser(String username) {
        String key = REDIS_DATABASE + ":" + REDIS_PREFIX + ":" + username;
        return (LexCmsUsers) redisService.get(key);
    }

    @Override
    public void setLexCmsFormUser(LexCmsUsers user) {
        String key = REDIS_DATABASE + ":" + REDIS_PREFIX + ":" + user.getAccount();
        redisService.set(key, user,REDIS_EXPIRE);
    }

    @Override
    public void setAllResourceList(List<LexCmsMenu> resourceList) {
        String key = REDIS_DATABASE + ":" + REDIS_PREFIX + ":" + REDIS_KEY_RESOURCE;
        redisService.set(key, resourceList,REDIS_EXPIRE);
    }

    @Override
    public void delAllResourceList() {
        String key = REDIS_DATABASE + ":" + REDIS_PREFIX + ":" + REDIS_KEY_RESOURCE;
        redisService.del(key);
    }

    @Override
    public List<LexCmsMenu> getAllResourceList() {
        String key = REDIS_DATABASE + ":" + REDIS_PREFIX + ":" + REDIS_KEY_RESOURCE;
        return (List<LexCmsMenu>) redisService.get(key);
    }

    @Override
    public void setMenuList(List<LexCmsMenu> menuList) {
        String key = REDIS_DATABASE + ":" + REDIS_PREFIX + ":" + "menu";
        redisService.set(key, menuList,REDIS_EXPIRE);
    }

    @Override
    public void delMenuList() {
        String key = REDIS_DATABASE + ":" + REDIS_PREFIX + ":" + "menu";
        redisService.del(key);
    }

    @Override
    public List<LexCmsMenu> getMenuList() {
        String key = REDIS_DATABASE + ":" + REDIS_PREFIX + ":" + "menu";
        return (List<LexCmsMenu>) redisService.get(key);
    }

    @Override
    public void setJwtBlackList(List<String> jwtBlackList) {
        String key = REDIS_DATABASE + ":" + REDIS_PREFIX + ":" + "jwtBlackList";
        redisService.set(key, jwtBlackList,REFRESH_EXPIRATION);
    }
    @Override
    public List<String> getJwtBlackList() {
        String key = REDIS_DATABASE + ":" + REDIS_PREFIX + ":" + "jwtBlackList";
        return (List<String>) redisService.get(key);
    }
}
